package main

import (
	"fmt"
)

// goroutine 流水线
func main() {
	input := []string{"a", "b", "c", "d", "a", "b", "c", "d"}
	c1 := make(chan string)
	c2 := make(chan string)
	go sourcGo(input, c1)
	go checkGo(c1, c2)
	resultGo(c2)
}

func sourcGo(sourcItems []string, c chan<- string) {
	for _, v := range sourcItems {
		c <- v
	}
	defer close(c)
}

func checkGo(c <-chan string, result chan<- string) {
	dic := make(map[string]int, len(c))
	defer close(result)

	for {
		v, ok := <-c
		if !ok {
			fmt.Println("c通道已经关闭")
			break
		}

		if _, ok := dic[v]; !ok {
			result <- v
		}
		dic[v]++
	}
}

func resultGo(result <-chan string) {
	for {
		select {
		case v, ok := <-result:
			if !ok {
				fmt.Println("result通道已经关闭")
				return
			}
			fmt.Println("移除掉重复多的数据：v=", v)
		}
	}

	// for {
	// 	v, ok := <-result
	// 	if !ok {
	// 		fmt.Println("result通道已经关闭")
	// 		return
	// 	}
	// 	fmt.Println("移除掉重复多的数据：v=", v)
	// }
}
